#include "constants.h";

XfMissionChapterExecute={
	private ["_chapter_succeeded"];
	diag_log "";
	diag_log "=========================================================================";
	diag_log "                             CHAPTER 1                                   ";
	diag_log "=========================================================================";
	diag_log "";
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Starting Chapter","_last_random_reinforcement","_random_reinf_diff","_reinforcement_power"];
	DM_Mission_Chapter_CanRun = True;
	DM_Mission_Chapter_Runs = True;
	DM_Mission_Chapter_Success = 0;
	call XfMissionChapterSetup;
	_chapter_succeeded = false;
	if (!DM_Mission_Mission_CanRun) exitWith {diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Mission canceled!"];};

	_random_town = DM_Chapter_Location;
	_random_town_pos = _random_town select XF_MAP_LOCATION_CENTER;
	_random_town_pos_size = _random_town select XF_MAP_LOCATION_SIZE;
	
	_players = call XPlayersNumber;
	_enemy_troops = 2 + _players + DM_Mission_EnemyPower + DM_Mission_EnemyCoeff;
	_reinf_timer = 600;
	switch (true) do {
		case (_players <= 5): { _reinf_timer = 600 };
		case (_players <= 10): { _reinf_timer = 400 };
		case (_players <= 15): { _reinf_timer = 300 };
		case (_players <= 20): { _reinf_timer = 240 };
		default {_reinf_timer = 140;};
	};	

	diag_log Format["(%1)[%2]: %3: %4: %5, %6",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Entering the loop",[], Time];
	while {DM_Mission_Chapter_CanRun AND DM_Mission_Mission_CanRun} do {
		sleep 10;
		_enemy_counts = ([_random_town_pos,_random_town_pos_size,d_enemy_side] call XfMissionGetZoneSideCount) select 0;
		
	};
	sleep 15;
	
	if (!isnil "DM_Ammo_Box") then {
		if (count DM_Ammo_Box == 3) then {
			["d_r_box", [DM_Ammo_Box]] call XNetCallEvent;
		};
	};
	
	if (DM_Mission_Mission_CanRun) then {
		if (_chapter_succeeded) then {
			call XfMissionChapterSuccess;
		} else {
			call XfMissionChapterFailure;
		};
	} else {
		diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Mission canceled!"];
	};
	sleep 30;
	_players_found = true;
	_enemy_count = 100;
	While {DM_Mission_Mission_CanRun && _enemy_count > 1 && _players_found} do {
		sleep 5;
		if (X_MP) then {
			_players_found = false;
			{
				if (([(getPos _x), _random_town_pos, _random_town_pos_size * 3] call XfPointInCircle) && isplayer _x) exitWith { _players_found = true; };
			}foreach playableUnits;
		} else {
			_players_found = ([(getPos player), _random_town_pos, _random_town_pos_size * 3] call XfPointInCircle);
		};
		_enemy_count = (([_random_town_pos,600,d_enemy_side] call XfMissionGetZoneSideCount) select 0);
		if (d_mission_debug_messages) then { diag_log Format["(%1)[%2]: %3: %4: player:%5  enemies:%6",diag_tickTime,"chapter1.sqf","XfMissionChapterExecute","Chapter End Check",_players_found,_enemy_count]; };
	};
	// Just in case a reinforcement is already on the way.
	call XfMissionCancelReinforcement;
	call XfMissionChapterCleanUp;
	DM_Mission_Chapter_Runs = False;
};

XfMissionChapterSetup={
	private ["_random_town","_random_town_pos","_random_town_pos_size","_troops_power","_troops_coeff","_grpSpecOps","_i"];
	diag_log Format["(%1)[%2]: %3; %4 %5",diag_tickTime,"chapter1.sqf","XfMissionChapterSetup","Starting Initialization:",DM_Mission_Name];
	
	_airplane_officer = [[15666.7,17116.8,0.0013175],2.72385];
	_branca_ammo = [15633.4,17345.8,0];
	
	[["%1\n\n%2","STR_MF_M_HAMMER1_5_NAME","STR_MF_M_HAMMER1_5_INTRO"],([22,00] call XfMissionGetDate),8,8] call XfMissionDisplayScreen;	
	sleep 3;
	DM_Ammo_Box = _branca_ammo;
	["d_m_box", [DM_Ammo_Box, (nearestObject [DM_Ammo_Box, "AllVehicles"]), "", d_own_side,"USVehicleBox_EP1"]] call XNetCallEvent;

	_random_town = [];
	_random_town_pos = [];
	_parameters = [d_map_locations];
	_parameters set [count _parameters, [XF_MAP_LOCATION_UNIQUENAME,"==","Ovallestan"]];
	_eligible_towns = _parameters call XfTableFilter;
	if ((count _eligible_towns) <= 0) exitWith {"STR_MF_RANDOMSITEFAILED" call XfMissionMessageToAll; DM_Mission_Mission_CanRun = false; DM_Mission_Chapter_CanRun = false;};
	_random_town = _eligible_towns select 0;
	_random_town_pos_size = _random_town select XF_MAP_LOCATION_SIZE;
	_random_town_pos_name = _random_town select XF_MAP_LOCATION_DISPLAYNAME;
	
	DM_Chapter_Location = _random_town;
	DM_Chapter_Position = _random_town_pos;
	
	// This indicates where the mission takes place
	[_random_town_pos,_random_town_pos_size,_random_town_pos_name] call XfMissionCurrentPosition;

	[false,["chapter1_random_town_zone", _random_town_pos, "ELLIPSE", "ColorRed", [_random_town_pos_size,_random_town_pos_size],"",0,"","BDiagonal",0.5]] call XfMissionMarker;
	
	["DefendTheTown","STR_MF_M_POGROM_TASK_DEFEND", ["STR_MF_M_POGROM_TASK_DEFEND_DESC",_random_town_pos_name],"",[],"Created"] call XfMissionTask;
};

XfMissionChapterSuccess={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterSuccess","Starting"];
	"STR_MF_M_POGROM_SUCCESS" call XfMissionMessageToAll;
	DM_Mission_Chapter_Success = 1;
};

XfMissionChapterFailure={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterFailure","Starting"];
	"STR_MF_M_POGROM_FAILURE" call XfMissionMessageToAll;
	DM_Mission_Chapter_Success = -1;
};

// Self-explanatory
XfMissionChapterCleanUp={
	diag_log Format["(%1)[%2]: %3; %4",diag_tickTime,"chapter1.sqf","XfMissionChapterCleanUp","Starting"];
	// Giving an empty position places the teleporter back to the hangar, closing the hangar.
	[[],0] call XfMissionPlaceStartTeleporter;
	[] call XfMissionCleanMarkers;
	[] call XfMissionCleanTriggers;
	[] call XfMissionCleanTasks;
};

DM_Mission_Chapter_Compiled = true;